import { Component, Input, ViewChild, ElementRef, OnInit, AfterViewInit } from '@angular/core';
import { ShapeBox } from '../model/component-model/component-related/shape-box-interface';
import { BoxService } from '../service/box.service';
import { ShapeBoxBase } from '../model/component-model/component-related/box-base';
import { EventEmitter } from '_@types_selenium-webdriver@2.53.43@@types/selenium-webdriver';
import { PointBase } from '../model/point-model/point-related/point-base';
import { Operation } from '../model/operation.model';
import { RootContainer } from '../model/component-model/container-model';

@Component({
    selector: 'shape-box',
    templateUrl: "./shape-box.component.html",
    styleUrls: ['./shape-box.component.css']
})

export class ShapeBoxComponent implements AfterViewInit {
    @Input() oper: Operation;
    @ViewChild('canvas') canvas: ElementRef;
    boxService: BoxService = BoxService.getService();

    constructor() {
    }
    ngAfterViewInit(): void {
        if (this.oper.hasCanvas) {
            let canvasEl: HTMLCanvasElement = this.canvas.nativeElement;
            let ctx: CanvasRenderingContext2D = canvasEl.getContext('2d');
            this.oper.ctx = ctx;
            this.oper.paint();
            if (this.oper instanceof ShapeBoxBase && !(this.oper instanceof RootContainer)) {
                this.boxService.boxs.push(this.oper);
            }
        }
    }
    in() {
        this.oper.in();
    }
    // select(event) {
    //     if(this.oper instanceof ShapeBoxBase){
    //         this.boxService.select(this.oper, event);
    //     }
    // }
    out() {
        if(this.oper instanceof ShapeBoxBase){
            if (!this.oper.selected) {
                this.oper.out();
            }
        }
    }
    pointmove(oper:Operation, event){
        oper.canvasMove(event);
    }
}